﻿

//加载AI数据
function load() {
    var clientid = getQueryString("clientid");
    var _staffid = getQueryString("staffid");
    ajaxGet(siteApiList.ClientAnalysis + "?clientid=" + clientid + "&staffid=" + _staffid, function (data) {
        if (!isJsonSuccess(data))
            return;
        if (data.Data == null) return;

        var xData3 = getDateArray(-15);
        var yData3 = getYData(data.Data.DateRecord, xData3);

        loadChart1(data.Data.ActionRecord);
        loadChart3(xData3, yData3);
        loadChart4(data.Data.ActionRecord);
    })
}



//饼图:客户兴趣占比
function loadChart1(arr) {
    var actionType = ',0,1,4';  //0 对商品感兴趣， 1 对官网感兴趣， 4 对我感兴趣
    var arrColor = new Array();
    var arrData = new Array();
    var total_num = 0;
    for (var i = 0; i < arr.length; i++) {
        var item = arr[i];
        if (actionType.indexOf(item.actionType) > -1) {
            total_num += item.Total;
            if (item.Total > 0) {
                arrData.push({ value: item.Total, name: value = "%", actionType: item.actionType });
                arrColor.push("");
            }
        }
    }

    //按actionType排序,从小到大
    for (var i = 0; i < arrData.length; i++) {
        for (var j = i + 1    ; j < arrData.length; j++) {
            if (arrData[i].actionType > arrData[j].actionType) {
                var temp = arrData[i];
                arrData[i] = arrData[j];
                arrData[j] = temp;
            }
        }
    }

    //更新百分比
    for (var i = 0; i < arrData.length; i++) {
        var item = arrData[i]
        arrData[i].name = parseFloat(item.value / total_num * 100).toFixed(2) + "%";

        if (item.actionType == 0)   
            arrColor[i] = "#ffab2b";        //黄色
        else if (item.actionType == 1)
            arrColor[i] = "#3fc3d2";        //蓝色
        else if (item.actionType == 4)
            arrColor[i] = "#ff5f1b";        //橙色
    }
 
    var option = {
        series: [{
            name: '访问来源',
            type: 'pie',
            color: arrColor,
            radius: '80%',
            data: arrData
        }]
    };

    var myChart = echarts.init(dom);
    myChart.setOption(option, true);
}

//15天内客户活跃度
function loadChart3(xData,yData) {
    var option3 = {
        title: {
            text: ''
        },
        xAxis: {
            splitLine: {
                show: false
            },
            axisTick: {
                show: false
            }, //是否显示刻度线
            type: 'category',
            boundaryGap: false,
            data: xData,
            boundaryGap: ['20%', '20%'] //两边留白
        },
        yAxis: {
            type: 'value',
            boundaryGap: [0, '100%'],
            splitLine: {
                show: false
            },
            axisTick: {
                show: false     //是否显示刻度线
            }, 
            min: 0,
            //max: 100,
            //axisLabel: { texts: [10, 40, 60, 80, 100] }
        },
        color: {
            type: 'linear',
            colorStops: [{
                offset: 1,
                color: '#FE7B00'
            }], // 100% 处的颜色
            globalCoord: false, // 缺省为 false
        },
        series: [{
            name: '模拟数据',
            type: 'line',
            smooth: true, //是否显示小圆点
            symbol: 'circle', //实体圆点
            symbolSize: 10, //圆点大小
            label: {
                normal: {
                    show: true,
                    position: 'top',
                    color: "#151D32" //标点文字颜色
                }
            },
            itemStyle: {
                normal: {
                    color: '#FA8429', //小圆点颜色
                    borderWidth: "2", //边框
                    borderColor: "#fff",
                    lineStyle: {
                        color: '#FD892D'
                    }
                }
            },
            data: yData,
        }]
    };
    var myChart3 = echarts.init(document.getElementById('main3'));
    myChart3.setOption(option3);
}

//互动图
function loadChart4(data) {
    var actionType = ',6,7,8,9,10';  //6 咨询产品，7 保存电话, 8 添加印象(名片靠谱)，9 拨打电话 , 10 标签点赞

    //按actionType排序,从小到大
    for (var i = 0; i < data.length; i++) {
        for (var j = i + 1    ; j < data.length; j++) {
            if (data[i].actionType > data[j].actionType) {
                var temp = data[i];
                data[i] = data[j];
                data[j] = temp;
            }
        }
    }

    //默认值,元素顺序固定
    var arrData  = [
             ['score', 'amount', 'product'], //颜色 x值 y值
             [0, 0, '咨询产品'],
             [0, 0, '保存电话'],
             [0, 0, '拨打电话'],
             [0, 0, '添加印象'],
             [0, 0, '点赞']
            ];

    
    for (var i = 0; i < data.length; i++) {
        var item = data[i];

        if (item.actionType == 6) {
            arrData[1][1] = item.Total;
        }
        else if (item.actionType == 7) {
            arrData[2][1] = item.Total;
        }
        else if (item.actionType == 8) {
            arrData[3][1] = item.Total
        }
        else if (item.actionType == 9) {
            arrData[4][1] = item.Total;
        }
        else if (item.actionType == 10) {
            arrData[5][1] = item.Total;
        }
    }


    var option4 = {
        dataset: {
            source: arrData
        },
        grid: {
            containLabel: true
        },
        xAxis: {
            splitLine: {
                show: false
            },
            axisTick: {
                show: false
            }, //是否显示刻度线
            name: ''
        },
        yAxis: {
            type: 'category',
            splitLine: {
                show: false
            },
            axisTick: {
                show: false
            }, //是否显示刻度线
        },
        visualMap: {
            show: false,
            orient: 'horizontal',
            left: 'center',
            min: 10,
            max: 100,
            text: ['High Score', 'Low Score'],
            // Map the score column to color
            dimension: 0,
            inRange: {
                color: ['#B2B2B2', '#FDC844', '#FEAB2B']
            }
        },
        series: [{
            type: 'bar',
            barWidth: 20,
            label: {
                normal: {
                    show: true,
                    position: 'right',
                    color: "#151D32" //标点文字颜色
                }
            },
            encode: {
                // Map the "amount" column to X axis.
                x: 'amount',
                // Map the "product" column to Y axis
                y: 'product'
            }
        }]
    };
    var myChart4 = echarts.init(document.getElementById('main4'));
    myChart4.setOption(option4);
}

//function formatArrayDate(arr, arrKey) {
//    var arrData = new Array();   
//    for (var i = 0; i < arr.length; i++) {
//        for (var key in arr[i]) {
//            if (key == arrKey) {
//                arrData.push(arr[i][key].substring(5,10));
//                break;
//            }
//        }
//    }
//    return arrData;
//}

function getYData(arr, xDada) {
    var arrData = new Array();
    //给日期段，设置Y值
    for (var i = 0; i < xDada.length; i++) {
        var date = xDada[i].replace("-", "");
        var y = getY(arr, date);
        arrData.push(y);
    }
    return arrData;
}

function getY(arr, date) {
    var y = 0;
    for (var i = 0; i < arr.length; i++) {
        var item = arr[i];
        if (item.DateNumber.substring(4).indexOf(date) > -1) {
            y = item.Total
            break;
        }
    }
    return y;
}


//function formatArray(arr, arrKey) {
//    var arrData = new Array();
//    for (var i = 0; i < arr.length; i++) {
//        for (var key in arr[i]) {
//            if (key == arrKey) {
//                arrData.push(arr[i][key]);
//                break;
//            }
//        }
//    }
//    return arrData;
//}


//返回指定的天数日期
function getDateArray(days) {
    var arrDate = new Array();
    var date = new Date()
    for (var i = days; i <= 0; i++) {
        arrDate.push(dateLater(date, i).newdates);
    }
    return arrDate;
}

function dateLater(dates, later) {
    let dateObj = {};
    let date = new Date(dates);
    date.setDate(date.getDate() + later);
    let day = date.getDay();
    dateObj.newdates = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    return dateObj;
}